package app.controller.city;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhou.ExcelExportor;

import app.entity.MeterDataBean;
import app.entity.MeterExcelData;
import app.service.ReadMeterService;
import app.utils.DataResponse;
@Controller
public class MeterController {
	
	//电表的dao
	@Autowired
	@Qualifier("ReadMeterService")
	private ReadMeterService service;
	
	/**
	 * 该方法用来获取指定电表在30内的数据，并且使用json的方式返回
	 *
	 * @param meterID 电表的的标识符
	 * @param response
	 */
	@RequestMapping(value="/meter/{meterID}/json/days",method=RequestMethod.GET)
	public void meterTendencyD(@PathVariable("meterID") String meterID,HttpServletResponse response){
		MeterDataBean meterTendendyD = service.getMeterTendendyD(meterID);
		DataResponse.jsonResponse(response, meterTendendyD);
	}
	
	/**
	 * 该方法用来获取指定电表六个月内的数据，并且使用json的方式返回
	 *
	 * @param meterID 电表的的标识符
	 * @param response
	 */
	@RequestMapping(value="/meter/{meterID}/json/months",method=RequestMethod.GET)
	public void meterTendencyM(@PathVariable("meterID") String meterID,HttpServletResponse response){
		MeterDataBean meterTendendyM = service.getMeterTendendyM(meterID);
		DataResponse.jsonResponse(response, meterTendendyM);
	}
	
	/**
	 * 用来获取全部电表的上个月的用电量表格
	 *
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/meter/excel/lastmonth",method=RequestMethod.GET)
	public void meterDataLastMonth(HttpServletResponse response) throws Exception{
		
		List<MeterExcelData> lastMonthData = service.getLastMonthData();
		InputStream excel = new ExcelExportor().getExcel(lastMonthData);
		Calendar calendar=Calendar.getInstance();
		Date date=new Date();
		date.setMonth(date.getMonth()-1);
		calendar.setTime(date);
		String name ="copy_meter_"+ calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+".xls";
		DataResponse.StreamResponse(response, excel, name);
	}
	
	public static void main(String[] args) throws Exception {
		new MeterController().meterDataLastMonth(null);
	}
	
	/**
	 * 
	 * @param days 使用path参数改参数用来指定
	 * @param response
	 */
	@RequestMapping(value="/meter/preday/{days}",method=RequestMethod.GET)
	public void meterNumPreDay(@PathVariable("days") Integer days,HttpServletResponse response){
		List<List<String>> meterNumPreDay = service.getMeterNumPreDay(days);
		DataResponse.jsonResponse(response, meterNumPreDay);
	}
	
}
